package edu.fdzc.learn.controller;

import edu.fdzc.learn.common.Result;
import edu.fdzc.learn.entity.Homework;
import edu.fdzc.learn.service.HomeworkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/homework")
public class HomeworkController {

    @Autowired
    private HomeworkService homeworkService;

    @PostMapping("/add")
    private Result addHomework(@RequestBody Homework homework) {
        homeworkService.add(homework);
        return Result.success();
    }

    @DeleteMapping("/{id}")
    private Result deleteHomework(@PathVariable int id) {
        homeworkService.deleteById(id);
        return Result.success();
    }

    @DeleteMapping("/delete/ids")
    private Result deleteByids(@RequestBody int[] ids)
    {
        homeworkService.deleteByIds(ids);
        return Result.success();
    }

    @PutMapping("/update")
    private Result updateHomework(@RequestBody Homework homework) {
        homeworkService.update(homework);
        return Result.success();
    }

    @GetMapping("/{id}")
    private Result<Homework> getHomeworkById(@PathVariable int id) {
        Homework h = homeworkService.findByid(id);
        return Result.success(h);
    }

    @GetMapping("/{courseId}/course")
    private Result<List<Homework>> getAllHomework(@PathVariable int courseId) {
        List<Homework> list = homeworkService.findAll(courseId);
        return Result.success(list);
    }

}
